#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: title02开心消消乐.py 
@version:
@time: 2023/05/27 
"""


def solve_method(n, m, matrix):
    times = 0
    for x in range(n):
        for y in range(m):
            cur = matrix[x][y]
            if cur == 1:
                times += 1
                changed = []
                changed.append([x, y])
                change(n, m, matrix, changed)
    print(times)


def change(n, m, matrix, changed):
    if not changed:
        return

    ints = changed.pop(0)
    directions = [
        (-1, -1),
        (-1, 1),
        (1, -1),
        (1, 1),
        (0, 1),
        (0, -1),
        (1, 0),
        (-1, 0)
    ]
    for d in directions:
        newX = ints[0] + d[0]
        newY = ints[1] + d[1]
        if 0 <= newX < n and 0 <= newY < m and matrix[newY][newY] == 1:
            matrix[newX][newY] = 0
            changed.append([newX, newY])
    change(n, m, matrix, changed)


n, m = map(int, input().split())
matrix = []
for x in range(n):
    row = list(map(int, input().split()))
    matrix.append(row)
solve_method(n, m, matrix)
